//
//  DXWYouXiViewController.m
//  pandaTV
//
//  Created by 淡馨慰 on 16/8/19.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWYouXiViewController.h"
#import "DXWYouxiCollectionViewLayout.h"
#import "DXWYouxiCell.h"
#import "DXWYouXiItem.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>

@interface DXWYouXiViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

/** collectionView */
@property (nonatomic, strong) UICollectionView *collectionView;
/** 游戏流水布局 */
@property (nonatomic, strong) DXWYouxiCollectionViewLayout *layout;
/** 游戏模型的数组 */
@property (nonatomic, strong) NSMutableArray *games;

@end


@implementation DXWYouXiViewController

static NSString * const  ID = @"youxiCell";
//懒加载 -- 模型数组
- (NSMutableArray *)games
{
    if (_games == nil) {
        _games = [NSMutableArray array];
    }
    return _games;
}

//懒加载 -- 游戏CollectionView
-(UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH) collectionViewLayout:self.layout];
        self.view.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

//懒加载 -- 游戏流水布局
- (DXWYouxiCollectionViewLayout *)layout
{
    if (_layout == nil) {
      _layout = [[DXWYouxiCollectionViewLayout alloc]init];
    }
    return _layout;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"游戏";
    
    //加载网络数据
    [self loadData];
    
    //collectionView
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"DXWYouxiCell" bundle:nil] forCellWithReuseIdentifier:ID];

    _collectionView.backgroundColor = [UIColor whiteColor];
    

}
#pragma mark -加载网络数据
- (void)loadData
{
    /*
      http://api.m.panda.tv/index.php?method=category.list&type=game&__version=1.1.7.1305&__plat=ios&__channel=appstore
     http://api.m.panda.tv/ajax_get_live_list_by_cate?cate=yzdr&pageno=1&pagenum=10&order=person_num&status=2&banner=1&__version=2.0.1.1339&__plat=ios&__channel=appstore
     */
    AFHTTPSessionManager *manager = [AFHTTPSessionManager dxw_HTTPManager];
    NSDictionary *dict = @{
                                @"method" : @"category.list",
                                @"type" : @"game",
                                @"__version" : @"1.1.7.1305",
                                @"__plat" : @"ios",
                                @"__channel" : @"appstore"
                            };
    [manager GET:@"http://api.m.panda.tv/index.php?" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _games = [DXWYouXiItem mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        
        //处理缺口
        [_collectionView reloadData];
        
        
//    [responseObject writeToFile:@"/Users/milkteacat/Desktop/淡/蛙喵项目练习/熊猫TV/youxi.plist" atomically:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

}

#pragma mark -UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _games.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DXWYouxiCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.item = _games[indexPath.row];
    return cell;
}





@end
